package com.lgj.web.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lgj.web.sys.mapper.SysResourceMapper;
import com.lgj.web.sys.model.SysResource;
import com.lgj.web.sys.service.SysResourceService;
import com.lgj.web.sys.service.SysUserCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 后台资源管理Service实现类
 * Created by macro on 2020/2/2.
 */
@Service
public class SysResourceServiceImpl implements SysResourceService {
    @Autowired
    private SysResourceMapper resourceMapper;
    @Autowired
    private SysUserCacheService userCacheService;
    @Override
    public int create(SysResource SysResource) {
        SysResource.setCreateTime(new Date());
        return resourceMapper.insert(SysResource);
    }

    @Override
    public int update(Long id, SysResource SysResource) {
        SysResource.setId(id);
        int count = resourceMapper.updateByPrimaryKeySelective(SysResource);
        userCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public SysResource getItem(Long id) {
        return resourceMapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        int count = resourceMapper.deleteByPrimaryKey(id);
        userCacheService.delResourceListByResource(id);
        return count;
    }

    @Override
    public  PageInfo list(Long categoryId, String nameKeyword, String urlKeyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<SysResource> list = resourceMapper.selectPageList(categoryId,nameKeyword,urlKeyword);
        PageInfo page = new PageInfo(list);
        return page;
    }

    @Override
    public List<SysResource> listAll() {
        return resourceMapper.selectAll();
    }
}
